//
//  RWForgetPasswordViewController.m
//  RedWine
//
//  Created by Liufangfang on 2016/12/31.
//  Copyright © 2016年 HuiBangKe. All rights reserved.
//

#import "RWForgetPasswordViewController.h"
#import "FTYInfoVertifyTools.h"
#import "RWFModifyPasswordViewController.h"

@interface RWForgetPasswordViewController ()<UITextFieldDelegate>{

    NSArray *imageIcons;
    NSArray *textFields;
}

@property (nonatomic, strong) UITextField *phoneNumberTF;
@property (nonatomic, strong) UITextField *codeTF;
@property (nonatomic, strong) NSString *phoneNumber;

@end

@implementation RWForgetPasswordViewController

#pragma mark - View LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initObjects];
    [self setNavigation];
    [self setForgetPasswordUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Load Datas

- (void)initObjects{
    
    imageIcons = @[@"myiphone_icon", @"code_icon"];
    textFields = @[@"请输入手机号", @"请输入验证码"];
}

#pragma mark - Config UI

- (void)setNavigation{
 
    self.title = @"修改密码";
}

- (void)setForgetPasswordUI{
    
    _forgetPasswordTV = [[FTYTouchTableView alloc] initWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, FTY_SCREEN_HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:_forgetPasswordTV];
    _forgetPasswordTV.backgroundColor = [FTYColor getEEEEEEColorWithAlpha:1.];
    _forgetPasswordTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    _forgetPasswordTV.scrollEnabled = NO;
    _forgetPasswordTV.dataSource = self;
    _forgetPasswordTV.delegate = self;
    _forgetPasswordTV.touchDelegate = self;
    _forgetPasswordTV.tableFooterView = [self setFooterUI];
}

#pragma mark - Config Invoke Methods

- (UIView *)setFooterUI{
    
    UIView *footerV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, 200.*FTY_HEIGHT_BASE)];
    
    FTYButton *nextBtn = [FTYButton createButtonWithTitle:@"确认" tColor:[UIColor blackColor] fontSize:15.*FTY_WIDTH_BASE backGroundColor:[FTYColor getMainColorWithAlpha:1.] cornerRadius:15.*FTY_WIDTH_BASE];
    [footerV addSubview:nextBtn];
    [nextBtn addTarget:self action:@selector(nextBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(40.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(10.*FTY_WIDTH_BASE);
        make.width.mas_equalTo(FTY_SCREEN_WIDTH - 15.*FTY_WIDTH_BASE *2);
        make.height.mas_equalTo(40.*FTY_HEIGHT_BASE);
    }];
    
    return footerV;
}

#pragma mark - Notificaiton Method

#pragma mark - Notificaiton Invoke Methods

#pragma mark - <UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 40.*FTY_HEIGHT_BASE;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 60.*FTY_HEIGHT_BASE;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString * const forgetPasswordID = @"forgetPasswordID";
    UITableViewCell *forgetPasswordCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:forgetPasswordID];
    forgetPasswordCell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSInteger row = indexPath.row;
    forgetPasswordCell.imageView.image = [UIImage imageNamed:imageIcons[row]];
    
    UITextField *textField = [[UITextField alloc] init];
    [forgetPasswordCell addSubview:textField];
    textField.placeholder = textFields[row];
    textField.font = FTY_FONTTSIZE(15);
    textField.delegate = self;
    textField.keyboardType = UIKeyboardTypeNumberPad;
    [textField sizeToFit];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(forgetPasswordCell);
        make.left.mas_equalTo(forgetPasswordCell.imageView.mas_right).offset(10.*FTY_WIDTH_BASE);
        make.right.mas_offset(-10.*FTY_WIDTH_BASE);
    }];
    
    if (0 == row) {
        _phoneNumberTF = textField;
    } else if (1 == row) {
        
        _codeTF = textField;
        FTYButton *getCodeBtn = [FTYButton createButtonWithTitle:@"获取验证码" tColor:[UIColor blackColor] fontSize:13.*FTY_WIDTH_BASE backGroundColor:[FTYColor getMainColorWithAlpha:1.] cornerRadius:15.*FTY_WIDTH_BASE];
        [forgetPasswordCell addSubview:getCodeBtn];
        [getCodeBtn addTarget:self action:@selector(getCodeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [getCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(forgetPasswordCell.mas_centerY);
            make.right.mas_equalTo(-10.*FTY_WIDTH_BASE);
            make.width.mas_equalTo(80.*FTY_WIDTH_BASE);
            make.height.mas_equalTo(30.*FTY_HEIGHT_BASE);
        }];
    }
    
    //底部线
    UIView *line = [[UIView alloc] init];
    [forgetPasswordCell addSubview:line];
    line.backgroundColor = [UIColor grayColor];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(.5*FTY_HEIGHT_BASE);
    }];
    
    return forgetPasswordCell;
}

#pragma mark - <UITableViewDelegate>

#pragma mark - <UITextFieldDelegate>

#pragma mark - <FTYTouchTableViewDelegate>

- (void)tableView:(UITableView *)tableView touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    [_phoneNumberTF resignFirstResponder];
    [_codeTF resignFirstResponder];
}

#pragma mark - Private Invoke Methods

- (void)getCodeBtnAction:(UIButton *)btn{

    _phoneNumber = _phoneNumberTF.text;

    [FTYInfoVertifyTools phoneNumberVertify:_phoneNumber showAtView:self.view];
    
    [FTYHTTPManager postInfoWithSubUrl:[NSString stringWithFormat:@"/shop/ybkapi.php?act=ybkapi&op=verify&type=updatepassword&member_mobile=%@", _phoneNumber] parameters:nil block:^(id result, NSError *error) {
        if (!error) {
            
            NSInteger code = [[[result objectForKey:@"err"] objectForKey:@"errorcode"] integerValue];
            NSString *info = [[result objectForKey:@"err"] objectForKey:@"errorinfo"];
            if (0 == code) {
                [[FTYToastView alloc] addTitle:@"验证码已发送～" addView:self.view];
            } else {
                [[FTYToastView alloc] addTitle:info addView:self.view];
            }
        } else {
            NSLog(@"=====%@", error);
        }
    }];
}

- (void)nextBtnAction:(UIButton *)btn{

    NSString *codeVer = _codeTF.text;
    NSString *phoneNumber = _phoneNumberTF.text;
    
    [FTYInfoVertifyTools phoneNumberVertify:phoneNumber showAtView:self.view];
    
    [FTYHTTPManager postInfoWithSubUrl:[NSString stringWithFormat:@"/shop/ybkapi.php?act=ybkapi&op=updatepwdbymobile&member_mobile=%@&verify=%@", phoneNumber, codeVer] parameters:nil block:^(id result, NSError *error) {
        if (!error) {
            
            NSInteger code = [[[result objectForKey:@"err"] objectForKey:@"errorcode"] integerValue];
            NSString *info = [[result objectForKey:@"err"] objectForKey:@"errorinfo"];
            if (0 == code) {
        
                RWFModifyPasswordViewController *fModifyPasswordVC = [[RWFModifyPasswordViewController alloc] init];
                fModifyPasswordVC.phoneNumber = phoneNumber;
                fModifyPasswordVC.codeVerify = codeVer;
                [self.navigationController pushViewController:fModifyPasswordVC animated:YES];
            } else {
                [[FTYToastView alloc] addTitle:info addView:self.view];
            }
        } else {
            NSLog(@"=====%@", error);
        }
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
